% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Replication "Deconstructing the Yield Curve"
% Crump and Gospodinov (2024)
% Date: 26-JUL-2024
% Code to make csv file for Table 7
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
clc;
%
fullSampleBR = load('../output/BR_B5000_BRdata1_rstarCPI0_197104_201801');
precrisisSampleBR = load('../output/BR_B5000_BRdata1_rstarCPI0_197104_200704');
%
fullSample = strings(12,5);
BRsubSample = strings(12,5);
precrisisSample = strings(12,5);
%
fullSample(:,1) = {"PC1", "", "PC2", "", "PC3", "", "$\pi^*$", "", "$r^*$", "", "$R^2$", "adj. $R^2$"};
BRsubSample(:,1) = {"PC1", "", "PC2", "", "PC3", "", "$\pi^*$", "", "$r^*$", "", "$R^2$", "adj. $R^2$"};
precrisisSample(:,1) = {"PC1", "", "PC2", "", "PC3", "", "$\pi^*$", "", "$r^*$", "", "$R^2$", "adj. $R^2$"};
%
dispPref = '%3.2f';
%
for i = 1:4
    fullSample(2*i-1,2) = num2str(round(fullSampleBR.betaHat1(i+1),2),dispPref);
    fullSample(2*i,2) = ['(', num2str(round(fullSampleBR.seHat1(i+1),2),dispPref), ')'];
    %
    fullSample(2*i-1,4) = num2str(round(fullSampleBR.betaHat41(i+1),2),dispPref);
    fullSample(2*i,4) = ['(', num2str(round(fullSampleBR.seHat41(i+1),2),dispPref), ')'];    
    %
    BRsubSample(2*i-1,2) = num2str(round(fullSampleBR.betaHat1_sub(i+1),2),dispPref);
    BRsubSample(2*i,2) = ['(', num2str(round(fullSampleBR.seHat1_sub(i+1),2),dispPref), ')'];
    %
    BRsubSample(2*i-1,4) = num2str(round(fullSampleBR.betaHat41_sub(i+1),2),dispPref);
    BRsubSample(2*i,4) = ['(', num2str(round(fullSampleBR.seHat41_sub(i+1),2),dispPref), ')'];        
    %
    precrisisSample(2*i-1,2) = num2str(round(precrisisSampleBR.betaHat1(i+1),2),dispPref);
    precrisisSample(2*i,2) = ['(', num2str(round(precrisisSampleBR.seHat1(i+1),2),dispPref), ')'];
    %
    precrisisSample(2*i-1,4) = num2str(round(precrisisSampleBR.betaHat41(i+1),2),dispPref);
    precrisisSample(2*i,4) = ['(', num2str(round(precrisisSampleBR.seHat41(i+1),2),dispPref), ')'];        
end
for i = 1:5
    fullSample(2*i-1,3) = num2str(round(fullSampleBR.betaHat2(i+1),2),dispPref);
    fullSample(2*i,3) = ['(', num2str(round(fullSampleBR.seHat2(i+1),2),dispPref), ')'];
    %
    fullSample(2*i-1,5) = num2str(round(fullSampleBR.betaHat42(i+1),2),dispPref);
    fullSample(2*i,5) = ['(', num2str(round(fullSampleBR.seHat42(i+1),2),dispPref), ')'];    
    %
    BRsubSample(2*i-1,3) = num2str(round(fullSampleBR.betaHat2_sub(i+1),2),dispPref);
    BRsubSample(2*i,3) = ['(', num2str(round(fullSampleBR.seHat2_sub(i+1),2),dispPref), ')'];
    %
    BRsubSample(2*i-1,5) = num2str(round(fullSampleBR.betaHat42_sub(i+1),2),dispPref);
    BRsubSample(2*i,5) = ['(', num2str(round(fullSampleBR.seHat42_sub(i+1),2),dispPref), ')'];    
    %
    precrisisSample(2*i-1,3) = num2str(round(precrisisSampleBR.betaHat2(i+1),2),dispPref);
    precrisisSample(2*i,3) = ['(', num2str(round(precrisisSampleBR.seHat2(i+1),2),dispPref), ')'];
    %
    precrisisSample(2*i-1,5) = num2str(round(precrisisSampleBR.betaHat42(i+1),2),dispPref);
    precrisisSample(2*i,5) = ['(', num2str(round(precrisisSampleBR.seHat42(i+1),2),dispPref), ')'];    
end

fullSample(end-1,2) = num2str(round(fullSampleBR.reg1.rsqr,2),dispPref);
fullSample(end-1,3) = num2str(round(fullSampleBR.reg2.rsqr,2),dispPref);
%
fullSample(end-1,4) = num2str(round(fullSampleBR.reg41.rsqr,2),dispPref);
fullSample(end-1,5) = num2str(round(fullSampleBR.reg42.rsqr,2),dispPref);
%
BRsubSample(end-1,2) = num2str(round(fullSampleBR.reg1_sub.rsqr,2),dispPref);
BRsubSample(end-1,3) = num2str(round(fullSampleBR.reg2_sub.rsqr,2),dispPref);
%
BRsubSample(end-1,4) = num2str(round(fullSampleBR.reg41_sub.rsqr,2),dispPref);
BRsubSample(end-1,5) = num2str(round(fullSampleBR.reg42_sub.rsqr,2),dispPref);
%
precrisisSample(end-1,2) = num2str(round(precrisisSampleBR.reg1.rsqr,2),dispPref);
precrisisSample(end-1,3) = num2str(round(precrisisSampleBR.reg2.rsqr,2),dispPref);
%
precrisisSample(end-1,4) = num2str(round(precrisisSampleBR.reg41.rsqr,2),dispPref);
precrisisSample(end-1,5) = num2str(round(precrisisSampleBR.reg42.rsqr,2),dispPref);
%
fullSample(end,2) = num2str(round(fullSampleBR.R2_1,2),dispPref);
fullSample(end,3) = num2str(round(fullSampleBR.R2_2,2),dispPref);
%
fullSample(end,4) = num2str(round(fullSampleBR.R2_41,2),dispPref);
fullSample(end,5) = num2str(round(fullSampleBR.R2_42,2),dispPref);
%
BRsubSample(end,2) = num2str(round(fullSampleBR.R2_1_sub,2),dispPref);
BRsubSample(end,3) = num2str(round(fullSampleBR.R2_2_sub,2),dispPref);
%
BRsubSample(end,4) = num2str(round(fullSampleBR.R2_41_sub,2),dispPref);
BRsubSample(end,5) = num2str(round(fullSampleBR.R2_42_sub,2),dispPref);
%
precrisisSample(end,2) = num2str(round(precrisisSampleBR.R2_1,2),dispPref);
precrisisSample(end,3) = num2str(round(precrisisSampleBR.R2_2,2),dispPref);
%
precrisisSample(end,4) = num2str(round(precrisisSampleBR.R2_41,2),dispPref);
precrisisSample(end,5) = num2str(round(precrisisSampleBR.R2_42,2),dispPref);
%
writematrix([fullSample(1:end-2,:) BRsubSample(1:end-2,2:end) precrisisSample(1:end-2,2:end)], '../output/TableBR.csv');
writematrix([fullSample(end-1:end,:) BRsubSample(end-1:end,2:end) precrisisSample(end-1:end,2:end)], '../output/TableBR_R2.csv');
